package com.example.jksj.bookmanagement.pro.mine.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jksj.bookmanagement.Base.BaseApplication;
import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.bean.BookLendHistoryBean;
import com.example.jksj.bookmanagement.utils.L;

import java.util.List;

public class LendHistoryAdapter extends RecyclerView.Adapter<LendHistoryAdapter.ViewHolder> {

    List<BookLendHistoryBean> bookList;

    /**
     * 构造方法
     * @param bookList 数据源
     */
    public LendHistoryAdapter(List<BookLendHistoryBean> bookList) {
        this.bookList = bookList;
    }

    /**
     * 创建ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 绑定ViewHolder，每加载一项都会调用一次
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BookLendHistoryBean bookBean = bookList.get(position);
        L.i(bookBean.toString());
        Glide.with(BaseApplication.getContext()).load(bookBean.getBookImgUrl()).into(holder.bookImage);
        holder.bookName.setText(bookBean.getBookName());
        holder.bookLendTime.setText(bookBean.getBookLendTime());
        holder.bookLendTrem.setText(bookBean.getBookLendTrem());
        holder.bookReturnTime.setText(bookBean.getBookLendReturnTime());

    }

    /**
     * 获取项数量
     * @return
     */
    @Override
    public int getItemCount() {
        return bookList.size();
    }

    /**
     * RecyclerView的持有者类
     */
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView bookImage;
        TextView bookName;
        TextView bookLendTime;
        TextView bookLendTrem;
        TextView bookReturnTime;

        public ViewHolder(View view){
            super(view);
            bookImage = view.findViewById(R.id.iv_historyBookImage);
            bookName = view.findViewById(R.id.tv_historyBookName);
            bookLendTime = view.findViewById(R.id.tv_historyBook_lendTime);
            bookLendTrem = view.findViewById(R.id.tv_historyBook_lendTerm);
            bookReturnTime = view.findViewById(R.id.tv_historyBook_returnTime);
        }

    }

}
